/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2020-10-09 17:57:01
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2020-09-18 15:18:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
const Controller = require('egg').Controller
let moment = require('moment');
class ETCController extends Controller {
  constructor(ctx) {
    super(ctx)


  }

  // 创建角色
  async getEtcDoor() {
    const { ctx } = this
    let etcDoorDate = await this.ctx.model.ValidateEtc.find({})

    ctx.helper.success({ ctx, res: { data: etcDoorDate } })
  }

  async getRoadBase() {
    const { ctx } = this
    let roadBase = await this.ctx.model.RoadBase.find({})
    ctx.helper.success({ ctx, res: { data: roadBase } })
  }

  async getRoadRealTime() {
    const { ctx } = this

    let { time } = ctx.request.body
    let timeString = time.substring(0, 18)
    console.log(timeString)
    let roadRealTime = await this.ctx.model.RoadRealTime.find({
      时间: {
        $regex: timeString, $options: 'i'
      }
    })
    ctx.helper.success({ ctx, res: { data: roadRealTime } })
  }

  async getRoadRealTimeById() {
    const { ctx } = this

    let { time, 路段ID } = ctx.request.body
    let timeString = time.substring(0, 18)
    console.log(timeString)
    let roadRealTime = await this.ctx.model.RoadRealTime.aggregate([
      {
        $match: {
          时间: {
            $regex: timeString, $options: 'i'
          },
        }
      },
      {
        $unwind: '$状态'
      },
      {
        $match: {
          '状态.路段ID': parseInt(路段ID)
        }
      },
    ])
    let roadItem = await this.ctx.model.RoadBase.findOne({ roadId: 路段ID })
    roadRealTime[0].状态.路段名称 = roadItem.roadBeginEtcName + '->' + roadItem.roadEndEtcName


    let responseData = {
      时间: roadRealTime[0].时间,
      ...roadRealTime[0].状态
    }

    ctx.helper.success({ ctx, res: { data: responseData } })
  }

  async getEtcStationRealTime() {
    const { ctx } = this

    let { time } = ctx.request.body
    let timeString = time.substring(0, 18)
    console.log(timeString)
    let etcStationRealTime = await this.ctx.model.EtcStationRealTime.find({
      时间: {
        $regex: timeString, $options: 'i'
      }
    })

    ctx.helper.success({ ctx, res: { data: etcStationRealTime } })
  }
  async getEtcStationRealTimeById() {
    const { ctx } = this

    let { time, ID号 } = ctx.request.body
    let timeString = time.substring(0, 18)
    console.log(timeString)

    let etcStationRealTime = await this.ctx.model.EtcStationRealTime.aggregate([
      {
        $match: {
          时间: {
            $regex: timeString, $options: 'i'
          },
        }
      },
      {
        $unwind: '$状态'
      },
      {
        $match: {
          '状态.ID号': ID号
        }
      },
    ])

    let etcDoorDate = await this.ctx.model.ValidateEtc.findOne({ FLAGID: ID号 })

    etcStationRealTime[0].状态.门架名称 = etcDoorDate.FLAGNAME

    let responseData = {
      时间: etcStationRealTime[0].时间,
      ...etcStationRealTime[0].状态
    }
    ctx.helper.success({ ctx, res: { data: responseData } })
  }

  async getCarRealTime() {
    const { ctx } = this

    let { time, lnt1, lat1, lnt2, lat2 } = ctx.request.body
    let timeString = time.substring(0, 18)
    console.log(timeString)
    // let carRealTime = await this.ctx.model.CarRealTime.find({
    //   时间: {
    //     $regex: timeString, $options: 'i'
    //   },
    //   // 经度: {
    //   //   $gte: parseFloat(lnt1),
    //   //   $lt: parseFloat(lnt2)
    //   // },
    //   // 纬度: {
    //   //   $gte: parseFloat(lat1),
    //   //   $lt: parseFloat(lat2)
    //   // },

    // }, '车牌号 车辆类型 经度 纬度 转向角')

    let carRealTime = await this.ctx.model.CarRealTime.aggregate([
      {
        $match: {
          时间: {
            $regex: timeString, $options: 'i'
          },
        },
      },
      {
        $project: {
          _id: 0,
          车牌号: 1,
          车辆类型: 1,
          转向角: 1,
          geometry: {
            type: 'POINT',
            coordinates: ['$经度', '$纬度']
          }
        },
      }

    ])

    ctx.helper.success({ ctx, res: { data: carRealTime } })
  }


  async getCarRealTimeById() {
    const { ctx } = this

    let { time, 车牌号 } = ctx.request.body
    let timeString = time.substring(0, 18)
    console.log(timeString)
    let carRealTime = await this.ctx.model.CarRealTime.aggregate([
      {
        $match: {
          时间: {
            $regex: timeString, $options: 'i'
          },
          '车牌号': 车牌号
        },
      },
      {
        $project: {
          _id: 0,
          车牌号: 1,
          车辆类型: 1,
          转向角: 1,
          车种: 1,
          运行时长: 1,
          途径ETC门架: 1,
          途径路段车速: 1,
          geometry: {
            type: 'POINT',
            coordinates: ['$经度', '$纬度']
          }
        },
      }

    ])
    ctx.helper.success({ ctx, res: { data: carRealTime } })
  }


  async getSummaryRealTime() {
    const { ctx } = this

    let { time } = ctx.request.body
    let timeString = time.substring(0, 18)
    console.log(timeString)
    let summaryReal = await this.ctx.model.SummaryRealTime.find({
      时间: {
        $regex: timeString, $options: 'i'
      }
    })
    summaryReal = JSON.parse(JSON.stringify(summaryReal))
    // let data = [{}]

    // data[0].time = time
    // data[0].运力排行 = [{
    //   "排名": 1,
    //   "起点": "G11",
    //   "终点": "G12",
    //   "运力": 44051
    // },
    // {
    //   "排名": 1,
    //   "起点": "G13",
    //   "终点": "G14",
    //   "运力": 44051
    // },
    // {
    //   "排名": 1,
    //   "起点": "G15",
    //   "终点": "G16",
    //   "运力": 44051
    // },
    // {
    //   "排名": 1,
    //   "起点": "G17",
    //   "终点": "G15",
    //   "运力": 44051
    // },
    // {
    //   "排名": 1,
    //   "起点": "G16",
    //   "终点": "G52",
    //   "运力": 44051
    // },
    // {
    //   "排名": 1,
    //   "起点": "G32",
    //   "终点": "G84",
    //   "运力": 44051
    // },
    // {
    //   "排名": 1,
    //   "起点": "G125",
    //   "终点": "G66",
    //   "运力": 44051
    // },
    // {
    //   "排名": 1,
    //   "起点": "G98",
    //   "终点": "G54",
    //   "运力": 44051
    // }]

    ctx.helper.success({ ctx, res: { data: summaryReal } })
  }
  async getMainRoadRealTime() {
    const { ctx } = this

    let { time } = ctx.request.body
    let timeString = time.substring(0, 18)
    console.log(timeString)
    let mainRoadRealTime = await this.ctx.model.MainRoadRealTime.find({
      时间: {
        $regex: timeString, $options: 'i'
      }
    })
    console.log(mainRoadRealTime)
    ctx.helper.success({ ctx, res: { data: mainRoadRealTime } })
  }

}


module.exports = ETCController